<?php
global $_W,$_GPC;
$op = empty($_GPC['op'])?'list':$_GPC['op'];
if($op == 'list'){
    $sql = 'select * from '.tablename($this->t_cardinfo).' where weid = :weid  order by `order` DESC';
    $params = [':weid'=>$_W['uniacid']];
    $list = pdo_fetchall($sql,$params);
}elseif($op == 'post'){
    $id = $_GPC['id'];
    $info = pdo_get($this->t_cardinfo,array('weid'=>$_W['uniacid'],'id'=>$id));
    if(checksubmit('submit')){
        $data['weid'] = $_W['uniacid'];
        $data['price'] = empty($_GPC['price'])?'0.00':round($_GPC['price'],2);
        $data['condition'] = $_GPC['condition'];
        $data['status'] = $_GPC['status'];
        $data['order'] = $_GPC['order'];
        $data['desc'] = $_GPC['desc'];
        $data['need'] = intval($_GPC['need']);
        $data['end_time'] = empty($_GPC['end_time'])?30:$_GPC['end_time'];
        if ($this->userid == $_W['uid']) {
            message($this->msg);
        }
        if(empty($id)){
            $data['createtime'] = time();
            $res = pdo_insert($this->t_cardinfo,$data);
        }else{
            $res = pdo_update($this->t_cardinfo,$data,array('id'=>$id));
        }
        if($res){
            message('操作成功',$this->createWebUrl('card'),'success');
        }else{
            message('操作失败',$this->createWebUrl('card'),'error');
        }
    }
}elseif($op == 'delete'){
    if ($this->userid == $_W['uid']) {
        message($this->msg);
    }
    $id = $_GPC['id'];
    $res = pdo_delete($this->t_cardinfo,array('id'=>$id));
    if($res){
        message('删除成功',$this->createWebUrl('card'),'success');
    }else{
        message('删除失败',$this->createWebUrl('card'),'error');
    }
}elseif($op == 'changestatus'){
    if ($this->userid == $_W['uid']) {
        $message = array('success'=>0,'mess'=>$this->msg);
        echo json_encode($message);exit;
    }
    $id = $_GPC['id'];
    $data['status'] = $_GPC['sta'];
    $info = pdo_get($this->t_cardinfo,array('id'=>$id,'weid'=>$_W['uniacid']));
    if($data['status'] == $info['status']){
        $message = array('success'=>0,'修改失败，请刷新页面后重新修改');
    }else{
        $res = pdo_update($this->t_cardinfo,$data,array('id'=>$id,'weid'=>$_W['uniacid']));
        if($res){
            $message = array('success'=>1,'mess'=>'修改成功');
        }else{
            $message = array('success'=>0,'mess'=>'修改失败');
        }
    }
    echo json_encode($message);exit;
}
include $this->template('web/card');
?>